package com.zgw.util;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Arrays;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import javax.annotation.PostConstruct;

import org.apache.commons.lang3.StringUtils;
import org.apache.zookeeper.ZooKeeper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.common.utils.CollectionUtils;
import com.google.common.collect.Lists;
import com.zgw.zookeep.ZookeepUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class CheckZkNode {

	private List<String> list = Lists.newArrayList();

	@Autowired
	ZookeepUtils zkUtils;

	@PostConstruct
	public void checkZkNode() {
		try {
			Timer timer = new Timer();
			timer.schedule(new TimerTask() {

				@Override
				public void run() {
					try {
						ZooKeeper zk = zkUtils.getZookeepCon();
						if (zkUtils.nodeIsExit(zk)) {
							String nodeData = zkUtils.getNodeData(zk);
							if (StringUtils.isNotBlank(nodeData)) {
								List<String> lists = Arrays.asList(nodeData.split("&"));
								lists.stream().forEach((v) -> {
									if (StringUtils.isNotBlank(v)) {
										String[] split = v.split(",");
										String ip = split[0];
										if (StringUtils.isNotBlank(ip)) {
											Socket socket = new Socket();
											String[] split2 = ip.split("/");											
											try {
												if(split2[2].contains(":")) {
													String[] split3 = split2[2].split(":");
													if(split3.length>=2) {
														socket.connect(
																new InetSocketAddress(split3[0], Integer.valueOf(split3[1])),
																3000);
													}else {
														int i=1/0;
													}
												}else {
													socket.connect(
															new InetSocketAddress(split2[2],0 ),
															3000);
												}												
												
											} catch (NumberFormatException | IOException e) {
//												e.printStackTrace();
												log.error(" 检测zk服务节点 服务连接失败  " + ip);
												list.add(v);
											} finally {
												try {
													socket.close();
												} catch (IOException e) {
													e.printStackTrace();
												}
											}
										}
									}
								});
								zkUtils.removeNode(list, zk);
								list.removeAll(list);
								if (CollectionUtils.isNotEmpty(list)) {
									log.info(" 检查所有节点正常  服务正常运行 !!! ");
								}
							} else
								log.error(
										" ######################  警告  ！！！ zookeep服务节点全部宕机服务全部下线   ###############################");
						} else
							log.error(
									" ######################  警告  ！！！ zookeep服务节点全部宕机服务全部下线   ###############################");
					} catch (Exception e) {
						e.printStackTrace();
						log.error(
								" ######################  警告  ！！！ 检测zookeep服务节点异常 可能存在全部服务宕机 {}   ###############################",
								e.getMessage());
					}
				}
			}, 1000l, 5000l);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(
					" ######################  警告  ！！！ 检测zookeep服务节点异常 可能存在全部服务宕机 {}   ###############################",
					e.getMessage());
		}
	}
}
